package com.banzhi.shop.utils;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banzhi.shop.R;

import java.util.Calendar;
import java.util.GregorianCalendar;

import cn.carbswang.android.numberpickerview.library.NumberPickerView;

/**
 * Created by HT on 2017/5/27.
 */

public class PersonDetailUtils {
    private static int ppwyear;
    private static int ppwmouth;
    private static int ppwday;
    private static NumberPickerView mPickerViewY;
    private static NumberPickerView mPickerViewM;
    private static NumberPickerView mPickerViewD;

    /**
     * 选择生日
     */
    public static void showbrithdayPopwidow(Activity activity, final TextView view) {
        View contentView = LayoutInflater.from(activity).inflate(R.layout.popupwindow_brithday, null);
        final PopupWindow popWindow = new PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
        popWindow.setContentView(contentView);

        popWindow.setAnimationStyle(R.style.AnimBottom);
        mPickerViewY = (NumberPickerView) contentView.findViewById(R.id.picker_year);
        mPickerViewM = (NumberPickerView) contentView.findViewById(R.id.picker_month);
        mPickerViewD = (NumberPickerView) contentView.findViewById(R.id.picker_day);
        final Button btntrue = (Button) contentView.findViewById(R.id.btn_true);
        final Button btncancel = (Button) contentView.findViewById(R.id.btn_cancel);
        RelativeLayout rlbackground = (RelativeLayout) contentView.findViewById(R.id.rl_background);
        RelativeLayout rl = (RelativeLayout) contentView.findViewById(R.id.rl);
        LinearLayout lltimerpicker = (LinearLayout) contentView.findViewById(R.id.time_picker);
        popWindow.setFocusable(true);
        popWindow.setOutsideTouchable(false);// 设置允许在外点击消失
        initTime();
        rlbackground.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });
        rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        lltimerpicker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        NumberPickerView.OnValueChangeListener nplistener = new NumberPickerView.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPickerView picker, int oldVal, int newVal) {
                switch (picker.getId()) {
                    case R.id.picker_year:
                        ppwyear = newVal;
                        initTime(ppwyear);
                        break;
                    case R.id.picker_month:
                        ppwmouth = newVal;
                        initTime(ppwyear, ppwmouth);
                        break;
                    case R.id.picker_day:
                        ppwday = newVal;
                        break;
                }

            }
        };
        mPickerViewY.setOnValueChangedListener(nplistener);
        mPickerViewM.setOnValueChangedListener(nplistener);
        mPickerViewD.setOnValueChangedListener(nplistener);
        btntrue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                view.setText(ppwyear + "-" + ppwmouth + "-" + ppwday);
                popWindow.dismiss();
            }
        });
        btncancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });

        ColorDrawable dw = new ColorDrawable(0x70000000);
        popWindow.setBackgroundDrawable(dw);
        popWindow.showAtLocation(contentView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);

    }

    /**
     * @param year
     * @param mouth 滑动月的时候调用
     */
    private static void initTime(int year, int mouth) {
        GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar.getInstance();
        calendar.set(year, mouth, 1);
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        int t = calendar.get(Calendar.DAY_OF_MONTH);
        if (ppwday > t) {
            ppwday = t;
        }
        setData(mPickerViewD, 1, t, ppwday);
    }


    /**
     * @param year 滑动年的时候调用
     */
    private static void initTime(int year) {
        GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar.getInstance();
        calendar.set(year, ppwmouth, 1);
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        int t = calendar.get(Calendar.DAY_OF_MONTH);
        if (ppwday > t) {
            ppwday = t;
        }
        setData(mPickerViewD, 1, t, ppwday);
    }


    /**
     * 第一次显示是时   获取显示当前的年月日
     */
    private static void initTime() {
        GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar.getInstance();
        ppwyear = calendar.get(Calendar.YEAR);
        ppwmouth = calendar.get(Calendar.MONTH) + 1;
        ppwday = calendar.get(Calendar.DAY_OF_MONTH);

        calendar.set(ppwyear, ppwmouth, 1);
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        int t = calendar.get(Calendar.DAY_OF_MONTH);

        setData(mPickerViewY, 1917, 2116, ppwyear);
        setData(mPickerViewM, 1, 12, ppwmouth);
        setData(mPickerViewD, 1, t, ppwday);
    }


    /**
     * @param picker
     * @param minValue
     * @param maxValue
     * @param value    设置最大，最小，当前年月日
     */
    private static void setData(NumberPickerView picker, int minValue, int maxValue, int value) {
        picker.setMinValue(minValue);
        picker.setMaxValue(maxValue);
        picker.setValue(value);
    }


}
